package main

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"time"
)

var redisClient *redis.Pool
func init(){
	maxActive  := 60000    // 不要超过文件句柄数   ulimt -n 查看文件句柄数
	maxIdle := 50000  // 最大空闲
	timeout := 1
	redisClient =&redis.Pool{
		MaxIdle: maxIdle,
		MaxActive:maxActive,
		IdleTimeout:time.Duration(timeout) * time.Second,
		Wait:true,
		Dial:func()(redis.Conn,error){
			conn,err := redis.Dial("tcp",
				"127.0.0.1:6379",
				redis.DialPassword("jinmingzhi"),
				redis.DialConnectTimeout(time.Duration(timeout) * time.Second),
				redis.DialReadTimeout(time.Duration(timeout) * time.Second),
				redis.DialWriteTimeout(time.Duration(timeout) * time.Second),
			)
			if err != nil{
				return nil,err
			}
			return conn,err
		},
	}
}


func main(){
	rc := redisClient.Get()
	defer rc.Close()
	if rc.Err() != nil{
		fmt.Println("获取失败")
		return
	}

	// 设置
	rc.Do("SET","k","v")
	s,_ := redis.String(rc.Do("GET","k"))
	fmt.Println(s)

	// 设置带时间的
	rc.Do("SET","name","jmz","EX","2")
	time.Sleep(2 * time.Second)
	name ,_ := redis.String(rc.Do("GET","name"))
	fmt.Println(name)

	//del
	rc.Do("SET","age","男")
	rc.Do("DEL","age")
	_,err := rc.Do("GET","age")
	if err != nil {
		fmt.Println("age数据被删除了")
	}

	// 检测值是否存在
	isKeyExit ,err := redis.Bool(rc.Do("EXISTS","name"))
	if err != nil {
		fmt.Printf("报错了：%v\n",err)
	} else {
		fmt.Printf("exists or not :%v\n",isKeyExit)
	}

	// HSet
	rc.Do("HSet","map","name","jmz")
	r,_:= redis.String(rc.Do("HGet","map","name"))
	fmt.Println(r)

	// list:
	rc.Do("lpush","booklist","abc","vvv","1000")
	rs,err := redis.Strings(rc.Do("lpop","booklist"))
	if err != nil{
		fmt.Println("lpop",err)   // 为什么在只取出来一个
	}

	fmt.Println(rs)

	// MSet
	rc.Do("MSet", "abc", 100, "efg", 300)
	re, err := redis.Ints(rc.Do("MGet", "abc", "efg"))
	fmt.Println(err)
	fmt.Println(re)
}




